﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Data;

namespace Novel.Common
{
    /// <summary>
    /// 枚举帮助类
    /// </summary>
    public partial class EnumHelper
    {
        #region 对应的KEY VALUE
        public class KeyValue
        {
            private string _text;
            private string _value;
            /// <summary>
            /// 文本
            /// </summary>
            public string Text
            {
                get { return this._text; }
                set { this._text = value; }
            }
            /// <summary>
            /// 值
            /// </summary>
            public string Value
            {
                get { return this._value; }
                set { this._value = value; }
            }
            /// <summary>
            /// 返回文本
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return _text;
            }
            /// <summary>
            /// 构造
            /// </summary>
            public KeyValue()
            {

            }
            /// <summary>
            /// 构造
            /// </summary>
            /// <param name="Text"></param>
            /// <param name="Value"></param>
            public KeyValue(string Text, string Value)
            {
                _text = Text;
                _value = Value;
            }
        }
        #endregion

        #region 获得某个Enum类型的绑定列表
        /// <summary>
        /// 
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static DataTable EnumListTable(Type enumType)
        {
            return EnumListTable<byte>(enumType);
        }
        /// <summary>
        /// 获得某个Enum类型的绑定列表
        /// </summary>
        /// <param name="enumType">枚举的类型，例如：typeof(Sex)</param>
        /// <returns>
        /// 返回一个DataTable
        /// DataTable 有两列：    "Text"    : System.String;
        ///                        "Value"    : System.Char
        /// </returns>
        public static DataTable EnumListTable<T>(Type enumType)
        {
            Check.Require(enumType.IsEnum == false, "参数类型错误");
            //if (enumType.IsEnum != true)
            //{    //不是枚举的要报错
            //    throw new InvalidOperationException();
            //}
            string cachekey = enumType.ToString();
            object o = CacheHelper.Get(cachekey);
            if (o != null && (o is DataTable))
            {
                return (DataTable)o;
            }

            //建立DataTable的列信息
            DataTable dt = new DataTable();
            dt.Columns.Add("Text", typeof(System.String));
            dt.Columns.Add("Value", typeof(System.String));

            //获得特性Description的类型信息
            Type typeDescription = typeof(DescriptionAttribute);

            System.Reflection.FieldInfo[] fields = enumType.GetFields();

            //检索所有字段
            string strValue = string.Empty;
            string strText = string.Empty;
            foreach (FieldInfo field in fields)
            {
                //过滤掉一个不是枚举值的，记录的是枚举的源类型
                if (field.FieldType.IsEnum == true)
                {
                    DataRow dr = dt.NewRow();

                    // 通过字段的名字得到枚举的值
                    dr["Value"] = ((T)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();

                    //获得这个字段的所有自定义特性，这里只查找Description特性
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                        dr["Text"] = aa.Description;
                    }
                    else
                    {
                        //如果没有特性描述,就显示英文的字段名
                        dr["Text"] = field.Name;
                    }
                    dt.Rows.Add(dr);
                }
            }
            CacheHelper.Set(cachekey, dt);

            return dt;
        }
        public static String[] GetValues(Type type)
        {
            Array values = Enum.GetValues(type);
            String[] results = new string[values.Length];

            for (Int32 i = 0; i < results.Length; i++)
            {
                results[i] = (Convert.ToInt32(values.GetValue(i))).ToString();
            }

            return results;
        }
        public static String[] ConvertToStrings(Array enums)
        {

            String[] results = new string[enums.Length];

            for (Int32 i = 0; i < results.Length; i++)
            {

                results[i] = (Convert.ToInt32(enums.GetValue(i))).ToString();
            }

            return results;
        }
        #endregion

        #region 获得某个Enum类型的绑定列表
        /// <summary>
        /// 获得某个Enum类型的绑定列表
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static List<KeyValue> EnumListKeyValue(Type enumType)
        {
            return EnumListKeyValue<byte>(enumType);
        }
        /// <summary>
        /// 获取某属性的ToString()值
        /// </summary>
        /// <param name="attributeType"></param>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static String GetAttributeValue(Type attributeType, Enum enumValue)
        {
            Type type = enumValue.GetType();

            FieldInfo fieldInfo = type.GetField(enumValue.ToString());
            if (fieldInfo == null) return null;

            Object[] attributes = fieldInfo.GetCustomAttributes(attributeType, true);

            if (attributes.Length == 0) return null;

            return attributes[0].ToString();
        }
        /// <summary>
        /// 获得某个Enum类型的绑定列表
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static List<KeyValue> EnumListKeyValue<T>(Type enumType)
        {
            Check.Require(enumType.IsEnum == false, "参数类型错误");
            //if (enumType.IsEnum != true)
            //{    //不是枚举的要报错
            //    throw new InvalidOperationException();
            //}
            string cachekey = "KEYVALUE_" + enumType.ToString();
            object o = CacheHelper.Get(cachekey);
            if (o != null)
            {
                return (List<KeyValue>)o;
            }

            List<KeyValue> list = new List<KeyValue>();
            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();

            string strValue = string.Empty;
            string strText = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum == true)
                {
                    KeyValue keyvalue = new KeyValue();
                    keyvalue.Value = ((T)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                        keyvalue.Text = aa.Description;
                    }
                    else
                    {
                        keyvalue.Text = field.Name;
                    }

                    list.Add(keyvalue);
                }
            }

            return list;
        }
        #endregion

        #region 获得某个Enum类型的中文翻译，从description中读取

        public static string GetEnumCNNameFromEnum(object val)
        {
            return GetEnumCNNameFromEnum(val.GetType(), val);
        }

        /// <summary>
        /// 获得某个Enum类型的中文翻译，从description中读取
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <param name="val">传入的值</param>
        /// <returns></returns>
        public static string GetEnumCNNameFromEnum(Type enumType, object val)
        {

            string enumvalue = Enum.GetName(enumType, val);
            if (enumvalue == string.Empty || enumvalue == null)
                return "";
            FieldInfo finfo = enumType.GetField(enumvalue);
            object[] cAttr = finfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (cAttr.Length > 0)
            {
                DescriptionAttribute desc = cAttr[0] as DescriptionAttribute;
                if (desc != null)
                {
                    return desc.Description;
                }
            }
            return enumvalue;
        }

        #endregion

    }
}
